// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports


namespace SCIEF.SysBFW.CommonExt
{
	public class ImageExt
	{
		/// <summary>
		/// Converts an image into an icon.
		/// </summary>
		/// <param name="img">The image that shall become an icon</param>
		/// <param name="size">The width and height of the icon. Standard
		/// sizes are 16x16, 32x32, 48x48, 64x64.</param>
		/// <param name="keepAspectRatio">Whether the image should be squashed into a
		/// square or whether whitespace should be put around it.</param>
		/// <returns>An icon!!</returns>
		public static System.Drawing.Icon ToIcon(System.Drawing.Image img, int size, bool keepAspectRatio)
		{
			
			System.Drawing.Bitmap square = new System.Drawing.Bitmap(size, size);
			// create new bitmap
			System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(square);
			// allow drawing to it
			int x = default(int);
			int y = default(int);
			int w = default(int);
			int h = default(int);
			
			if (img == null)
			{
				return null;
			}
			
			// dimensions for new image
			if (!keepAspectRatio || img.Height == img.Width)
			{
				// just fill the square
				x = 0;
				y = 0;
				// set x and y to 0
				// set width and height to size
				w = size;
				h = size;
			}
			else
			{
				// work out the aspect ratio
				float r = (float) ((img.Width) / img.Height);
				
				// set dimensions accordingly to fit inside size^2 square
				if (r > 1)
				{
					// w is bigger, so divide h by r
					w = size;
					h = (int) (Math.Truncate((decimal) ((size) / r)));
					x = 0;
					// center the image
					y = System.Convert.ToInt32((size - h) / 2);
				}
				else
				{
					// h is bigger, so multiply w by r
					w = (int) (Math.Truncate((decimal) ((size) * r)));
					h = size;
					y = 0;
					// center the image
					x = System.Convert.ToInt32((size - w) / 2);
				}
			}
			
			// make the image shrink nicely by using HighQualityBicubic mode
			g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
			g.DrawImage(img, x, y, w, h);
			// draw image with specified dimensions
			g.Flush();
			// make sure all drawing operations complete before we get the icon
			// following line would work directly on any image, but then
			// it wouldn't look as nice.
			return System.Drawing.Icon.FromHandle(square.GetHicon());
		}
		
	}
	
}
